package com.protocols.sctp.m3ua;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;

import com.protocols.*;

public class AffectedPointCode implements Serializable{
	private OctaveInteger ParameterTag= new OctaveInteger(0x12,2); // Affected Point Code
	private OctaveInteger ParameterLength= new OctaveInteger(8,2);	// Length=8
	private byte[] AffectedPC= new byte[0];
	private Boolean Visible=false;
	private Boolean Mandatory =true;
	

	public AffectedPointCode(Boolean ismandatory) {
		Mandatory=ismandatory;
	}
	

	public AffectedPointCode(int mask, int pointcode,Boolean mandatory) {		
		Mandatory=mandatory;		
		addAffectedPointCode(mask, pointcode);
		Visible=true;
		
	}
	
	public void addAffectedPointCode(int mask, int pointcode) {	
		OctaveInteger msk= new OctaveInteger(mask,1);
		OctaveInteger pc= new OctaveInteger(pointcode,2);
		AffectedPC= ProtoOperations.concatArrays(AffectedPC, msk.getByteArray(), pc.getByteArray());
		Visible=true;
		
	}
	
	public void setAffectedPointCode(byte[] pointcode) {	
		AffectedPC= pointcode;
		Visible=true;
		
	}
	
	
	public byte [] getByteArray(){		
		return  !Visible?new byte[0]:ProtoOperations.concatArrays(ParameterTag.getByteArray(), ParameterLength.getByteArray(), AffectedPC);
		

	}
	
	public Boolean isMandatory() {
		return Mandatory;
	}

	public void setMandatory(Boolean mandatory) {
		Mandatory = mandatory;
	}
	
	
	
	public int getLength(){

		return getByteArray().length;
		
	}


	public int getParameterTag() {
		return ParameterTag.Value;
	}


	public byte [] decode(byte [] input) throws ProtocolStackException{
		
		if (input.length==0)
			return new byte[0];
		else if (input.length<4 && input.length>0)
			throw new ProtocolStackException("Decode Failure:InfoString"+ProtoOperations.getHexString(input));
		
		
		if (input[0]==0 && input[1]==7)
		{			
			ParameterLength.Value=ProtoOperations.intFromByteArray(input, 2, 2);
			setAffectedPointCode( ProtoOperations.extractByteArray(input, 4, ParameterLength.Value-4));
			return ProtoOperations.extractByteArray(input, ParameterLength.Value, input.length-ParameterLength.Value);
		}
		else if (isMandatory())
			throw new ProtocolStackException("Decode DiagonosticInfo"+ProtoOperations.getHexString(input));
		else
			return input;
				
	}


	public int getParameterLength() {
		return Visible?ParameterLength.Value:0;
	}




	public byte[] getAffectedPC() {
		return AffectedPC;
	}

	

		
		
}
